﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;

namespace ResearchId.Validation
{
    public partial class PhysicalAddress : System.Web.UI.Page
    {
        private const int _groupId = 2;
        private const ClaimEnum _claimId_Address = ClaimEnum.Address;
        private const string _claimId_Address_datatype = "System.String";
        private const ClaimEnum _claimId_City = ClaimEnum.City;
        private const string _claimId_City_datatype = "System.String";
        private const ClaimEnum _claimId_State = ClaimEnum.State;
        private const string _claimId_State_datatype = "System.String";
        private const ClaimEnum _claimId_ZipCode = ClaimEnum.ZipCode;
        private const string _claimId_ZipCode_datatype = "System.String";
        private const ClaimEnum _claimId_County = ClaimEnum.Country;
        private const string _claimId_County_datatype = "System.String";
        private string _stateText = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCountry();
                ddlCountry.SelectedValue = Country.GetUsaCountryId.ToString();
                BindProvinces();
                LoadData();
                if (_stateText != "")
                    ddlState.Items.FindByText(_stateText).Selected = true;
            }
        }
        private void BindCountry()
        {
            ddlCountry.DataSource = Country.GetCountry();
            ddlCountry.DataTextField = "CountryName";
            ddlCountry.DataValueField = "CountryId";
            ddlCountry.DataBind();
            
        }
        private void BindProvinces()
        {
            List<Province> provinces = Province.GetProvince(Int32.Parse(ddlCountry.SelectedValue));
            Province p = new Province();
            p.Name = "Select State/Province ...";
            p.ProvinceId = 0;
            provinces.Insert(0, p);
            ddlState.DataSource = provinces;
            ddlState.DataTextField = "Name";
            ddlState.DataValueField = "ProvinceId";
            ddlState.DataBind();
        }
        private void LoadData()
        {
            Group myGroup = Group.GetGroup(_groupId);
            foreach (GroupClaim myClaim in myGroup.GroupClaims)
            {
                IndividualClaim myIndividualClaim = IndividualClaim.GetIndividualClaim(
                    (ClaimEnum)myClaim.ClaimId
                    , Individual.GetIndividual(Page.User.Identity.Name).IndividualId);
                if (myIndividualClaim != null)
                {
                    switch ((ClaimEnum)myIndividualClaim.ClaimId)
                    {
                        case _claimId_Address:
                            tbAddressLine1.Text = myIndividualClaim.Value;
                            break;
                        case _claimId_City:
                            tbCity.Text = myIndividualClaim.Value;
                            break;
                        case _claimId_County:
                            ddlCountry.Items.FindByText(myIndividualClaim.Value).Selected = true;
                            BindProvinces();
                            break;
                        case _claimId_State:
                            _stateText = myIndividualClaim.Value;
                            break;
                        case _claimId_ZipCode:
                            tbZipCode.Text = myIndividualClaim.Value;
                            break;
                    }
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("..\\default.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string UserName = Page.User.Identity.Name;
            Individual i = Individual.GetIndividual(UserName);
            IndividualClaim.CreateIndividualClaim(
                i.IndividualId
                , _claimId_Address
                , tbAddressLine1.Text
                , _claimId_Address_datatype);
            IndividualClaim.CreateIndividualClaim(
                i.IndividualId
                , _claimId_City
                , tbCity.Text
                , _claimId_City_datatype);
            IndividualClaim.CreateIndividualClaim(
                i.IndividualId
                , _claimId_County
                , ddlCountry.SelectedItem.Text
                , _claimId_County_datatype);
            IndividualClaim.CreateIndividualClaim(
                i.IndividualId
                , _claimId_State
                , ddlState.SelectedItem.Text
                , _claimId_State_datatype);
            IndividualClaim.CreateIndividualClaim(
                i.IndividualId
                , _claimId_ZipCode
                , tbZipCode.Text
                , _claimId_ZipCode_datatype);
        }

        protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindProvinces();
        }

        

    }
}
